package com.tony.blog.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.tony.blog.api.utils.Constant;
import com.tony.blog.model.entity.SysDeptEntity;
import com.tony.blog.model.mapper.SysDeptMapper;
import com.tony.blog.server.service.CommonDataService;
import com.tony.blog.server.service.SysDeptService;
import com.tony.blog.server.shiro.ShiroUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysDeptControllerImpl
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/19/20:46
 * @Description:
 */
@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDeptEntity> implements SysDeptService {

    private static final Logger log = LoggerFactory.getLogger(SysDeptServiceImpl.class);

    @Autowired
    private CommonDataService commonDataService;

    /**
    * @Description: queryAll 获取部门列表
    * @Param: [map]
    * @return: java.util.List<com.tony.pmp.model.entity.SysDeptEntity>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/19 21:15
    */
    @Override
    public List<SysDeptEntity> queryAll(Map<String, Object> map) {
        //region 无权限控制的获取

        /*return baseMapper.queryList(map);*/

        //endregion

        //region 有权限控制的获取

        //非超级管理员
        if (ShiroUtil.getUserId() != Constant.SUPER_ADMIN) {
            //将部门数据id列表转化为id拼接的字符串
            String deptDataIds = commonDataService.getCurrUserDataDeptIdsStr();
            map.put("deptDataIds",(StringUtils.isNotBlank(deptDataIds))? deptDataIds : null);
        }
        return baseMapper.queryListByComission(map);

        //endregion
    }

    /**
    * @Description: queryDeptIds 获取当前部门的子部门id列表
    * @Param: [deptId]
    * @return: java.util.List<java.lang.Long>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/20 13:39
    */
    @Override
    public List<Long> queryDeptIds(Long parentId) {
        return baseMapper.queryDeptIds(parentId);
    }

    /**
    * @Description: getSubDeptIdList 获取当前部门的子部门id列表
    * @Param: [deptId]
    * @return: java.util.List<java.lang.Long>
    * @Author: 皮蛋布丁
    * @Date: 2021/7/29 15:11
    */
    @Override
    public List<Long> getSubDeptIdList(Long deptId) {
        List<Long> deptIdList = Lists.newLinkedList();

        //第一级部门id列表
        List<Long> subIdList = baseMapper.queryDeptIds(deptId);
        //递归
        getDeptTreeList(subIdList,deptIdList);
        return deptIdList;
    }

    /**
    * @Description: getDeptTreeList 递归
    * @Param: [subIdList 第一级部门数据id列表, deptIdList 每次递归时循环存储的结果数据id列表]
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/29 15:23
    */
    private void getDeptTreeList(List<Long> subIdList,List<Long> deptIdList) {
        List<Long> list;
        for (Long subId : subIdList) {
            list = baseMapper.queryDeptIds(subId);
            if (list != null && !list.isEmpty()) {
                //调用递归之处
                getDeptTreeList(list,deptIdList);
            }
            //执行到此表示递归结束
            deptIdList.add(subId);
        }
    }
}
